package cn.bgs.serviceedu.MyConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/8/23-10:16
 */
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(Environment environment){
        return  new Docket(DocumentationType.SWAGGER_2).groupName("别人的API文档");

    }

    //配置Swagger2
    @Bean
    public Docket docket(Environment environment){

        //获取配置文件中的环境，
        Profiles profiles = Profiles.of("dev", "test","pro");
        //如果是测试环境test或者是开发环境dev，就为true，否则就为false发布环境pro,将对象放入enable中判断
        boolean pro = environment.acceptsProfiles(profiles);
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //basePackage扫描指定包
                //any扫描全部
                //none全部不扫描
                //withClassAnnotation扫描带有指定注解的Controller
                //withMethodAnnotation扫描带有指定注解的方法
                .apis(RequestHandlerSelectors.any())
                //ant 扫描带有指定 ""的controller
                .paths(PathSelectors.any())
                .build()
                .enable(pro)
                .groupName("亦辰的API文档");

    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("","","");
        return new ApiInfo(
                "亦辰的API文档",
                "YiChen",
                "v1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}
